Connectivity Visualizations¶
Connectivity Plot¶

from simpl_eeg import connectivity, eeg_objects
import warnings
warnings.filterwarnings('ignore')
Note
Please include the line below in your IDE so that the changes would be simultaneously reflected when you make a change to the python scripts.**
%load_ext autoreload
%autoreload 2
Define parameters¶
A detailed description of all parameters can be found in the connectivity.animate_connectivity docstring:
help(connectivity.animate_connectivity)
Help on function animate_connectivity in module simpl_eeg.connectivity:
animate_connectivity(epoch, calc_type='correlation', steps=20, pair_list=[], threshold=0, show_sphere=True, colormap='RdBu_r', vmin=None, vmax=None, line_width=None, title=None, **kwargs)
Animate 2d EEG nodes on scalp with lines representing connectivity
Args:
epochs (mne.epochs.Epochs): Epoch to visualize
calc_type (str: Connectivity calculation type. Defaults to "correlation".
pair_list ([str], optional): List of node pairs. Defaults to [], which indicates all pairs.
threshold (int, optional): Unsigned connectivity threshold to display connection. Defaults to 0.
show_sphere (bool, optional): Whether to show the cartoon head or not. Defaults to True.
steps (int, optional): Number of frames to use in correlation caluclation. Defaults to 20.
colormap (str, optional): Colour scheme to use. Defaults to "RdBu_r"
vmin (int, optional): The minimum for the scale. Defaults to None.
vmin (int, optional): The maximum for the scale. Defaults to None.
line_width (int, optional): The line width for the connections. Defaults to None for non-static width.
title (str, optional): The title to display on the plot. Defaults to None for no title.
**kwargs (dict, optional): Optional arguments to pass to mne.viz.plot_sensors().
Returns:
matplotlib.animation.Animation: Animation of connectivity plot
# change values below to values of interest
experiment_path = "../../data/927" # path to the experiment folder.
nth_epoch = 0
# connectivity parameters
vmin = -1
vmax = 1
colormap = "RdBu_r"
calc_type = "correlation"
pair_list = [] # select from the PAIR_OPTIONS below or use a custom pair.
line_width = None
steps = 50
threshold = 0
show_sphere = True
PAIR_OPTIONS = {
"all_pairs": [],
"local_anterior": "Fp1-F7, Fp2-F8, F7-C3, F4-C4, C4-F8, F3-C3",
"local_posterior": "T5-C3, T5-O1, C3-P3, C4-P4, C4-T6, T6-O2",
"far_coherence": "Fp1-T5, Fp2-T6, F7-T5, F7-P3, F7-O1, T5-F3, F3-P3, F4-P4, P4-F8, F8-T6, F8-O2, F4-T6",
"prefrontal_to_frontal_and_central": "Fp1-F3, Fp1-C3, Fp2-F4, Fp2-C4",
"occipital_to_parietal_and_central": "C3-O1, P3-O1, C4-O2, P4-O4",
"prefrontal_to_parietal": "Fp1-P3, Fp2-P4",
"frontal_to_occipital": "F3-O1, P4-O2",
"prefrontal_to_occipital": "Fp1-O1, Fp2-O2"
}
Create epoched data¶
For additional options see Creating EEG Objects section.
epochs = eeg_objects.Epochs(experiment_path)
epoch = epochs.get_nth_epoch(0)
Reading /Users/sasha/mds/simpl_eeg_capstone/data/927/fixica.fdt
Not setting metadata
Not setting metadata
33 matching events found
Applying baseline correction (mode: mean)
0 projection items activated
Loading data for 33 events and 2049 original time points ...
0 bad epochs dropped
Create the connectivity plot¶
Generating the animation¶
%matplotlib inline
%%capture
anim = connectivity.animate_connectivity(
epoch,
calc_type=calc_type,
steps=steps,
pair_list=pair_list,
threshold=threshold,
show_sphere=show_sphere,
colormap=colormap,
vmin=vmin,
vmax=vmax,
line_width=line_width,
)
from IPython.display import HTML
html_plot = anim.to_jshtml()
video = HTML(html_plot)
video
Saving the animation¶
Save as html¶
html_file_path = "../../exports/examples/connectivity.html" # change the file path to where you would like to save the file
html_file = open(html_file_path, "w")
html_file.write(html_plot)
html_file.close()
Save as gif¶
%%capture
anim_conn = connectivity.animate_connectivity(epoch, vmin=-1, vmax=1, pair_list=PAIR_OPTIONS["far_coherence"])
gif_file_path = "../../exports/examples/connectivity.gif" # change the file path to where you would like to save the file
anim_conn.save(gif_file_path, fps=3, dpi=300) # set frames per second (fps) and resolution (dpi)
Save as mp4¶
mp4_file_path = "../../exports/examples/connectivity.mp4"
anim_conn.save(mp4_file_path, fps=3, dpi=300)
Note
If FFMpegWriter does not work on your computer you can save the file as a gif first and then convert it into mp4 file.
import moviepy.editor as mp
clip = mp.VideoFileClip(gif_file_path) # change the file path to where you saved the gif file
clip.write_videofile(mp4_file_path) # change the file path to where you would like to save the file
t: 0%| | 0/102 [00:00<?, ?it/s, now=None]
Moviepy - Building video ../../exports/examples/connectivity.mp4.
Moviepy - Writing video ../../exports/examples/connectivity.mp4
t: 2%|▏ | 2/102 [00:00<00:15, 6.41it/s, now=None]
t: 4%|▍ | 4/102 [00:00<00:12, 7.70it/s, now=None]
t: 8%|▊ | 8/102 [00:00<00:09, 10.12it/s, now=None]
t: 12%|█▏ | 12/102 [00:00<00:07, 12.66it/s, now=None]
t: 18%|█▊ | 18/102 [00:00<00:05, 15.82it/s, now=None]
t: 21%|██ | 21/102 [00:01<00:05, 15.67it/s, now=None]
t: 27%|██▋ | 28/102 [00:01<00:03, 20.39it/s, now=None]
t: 35%|███▌ | 36/102 [00:01<00:02, 26.11it/s, now=None]
t: 43%|████▎ | 44/102 [00:01<00:01, 32.68it/s, now=None]
t: 49%|████▉ | 50/102 [00:01<00:01, 29.99it/s, now=None]
t: 54%|█████▍ | 55/102 [00:01<00:01, 33.76it/s, now=None]
t: 61%|██████ | 62/102 [00:01<00:01, 39.21it/s, now=None]
t: 67%|██████▋ | 68/102 [00:01<00:00, 41.87it/s, now=None]
t: 73%|███████▎ | 74/102 [00:02<00:00, 44.65it/s, now=None]
t: 78%|███████▊ | 80/102 [00:02<00:00, 48.24it/s, now=None]
t: 84%|████████▍ | 86/102 [00:02<00:00, 44.83it/s, now=None]
t: 89%|████████▉ | 91/102 [00:02<00:00, 45.01it/s, now=None]
t: 95%|█████████▌| 97/102 [00:02<00:00, 46.79it/s, now=None]
Moviepy - Done !
Moviepy - video ready ../../exports/examples/connectivity.mp4
Connectivity Circle Plot¶

Define parameters¶
A detailed description of all parameters can be found in the connectivity.animate_connectivity_circle docstring:
help(connectivity.animate_connectivity_circle)
Help on function animate_connectivity_circle in module simpl_eeg.connectivity:
animate_connectivity_circle(epoch, calc_type='correlation', max_connections=50, steps=20, colormap='RdBu_r', vmin=None, vmax=None, line_width=None, title=None, **kwargs)
Animate connectivity circle
Args:
epoch (mne.epochs.Epochs): Epoch to visualize
calc_type (str, optional): Connectivity calculation type. Defaults to "correlation".
max_connections (int, optional): Number of connections to display. Defaults to 50.
steps (int, optional): Number of frames to use in correlation caluclation. Defaults to 20.
colormap (str, optional): Colour scheme to use. Defaults to "RdBu_r".
vmin (int, optional): The minimum for the scale. Defaults to None.
vmin (int, optional): The maximum for the scale. Defaults to None.
line_width (int, optional): The line width for the connections. Defaults to None for non-static width.
title (str, optional): The title to display on the plot. Defaults to None for no title.
**kwargs (dict, optional): Optional arguments to pass to mne.viz.plot_connectivity_circle().
Returns:
matplotlib.animation.Animation: Animation of connectivity plot
# change values below to values of interest
# connectivity circle parameters
vmin = -1
vmax = 1
colormap = "RdBu_r"
calc_type = "correlation"
line_width = 1
steps = 50
max_connections = 50
Generating the animation¶
%matplotlib notebook
%%capture
anim = connectivity.animate_connectivity_circle(
epoch,
calc_type=calc_type,
max_connections=max_connections,
steps=steps,
colormap=colormap,
vmin=vmin,
vmax=vmax,
line_width=line_width,
)
from IPython.display import HTML
html_plot = anim.to_jshtml()
video = HTML(html_plot)
video
Saving the animation¶
Save as html¶
html_file_path = "../../exports/examples/connectivity_circle.html" # change the file path to where you would like to save the file
html_file = open(html_file_path, "w")
html_file.write(html_plot)
html_file.close()
Save as gif¶
%%capture
anim_cir = connectivity.animate_connectivity_circle(epoch)
gif_file_path = "instruction_imgs/connectivity_circle.gif" # change the file path to where you would like to save the file
anim_cir.save(gif_file_path, fps=3, dpi=300)
Save as mp4¶
%%capture
mp4_file_path = "../../exports/examples/connectivity_cicle.mp4"
anim_cir.save(mp4_file_path, fps=3, dpi=300)
Note
If FFMpegWriter does not work on your computer you can save the file as a gif first and then convert it into mp4 file.
import moviepy.editor as mp
clip = mp.VideoFileClip(gif_file_path) # change the file path to where you saved the gif file
clip.write_videofile(mp4_file_path) # change the file path to where you would like to save the file
t: 0%| | 0/20 [00:00<?, ?it/s, now=None]
Moviepy - Building video ../../exports/examples/connectivity_cicle.mp4.
Moviepy - Writing video ../../exports/examples/connectivity_cicle.mp4
t: 10%|█ | 2/20 [00:00<00:02, 8.40it/s, now=None]
t: 40%|████ | 8/20 [00:00<00:01, 11.25it/s, now=None]
t: 75%|███████▌ | 15/20 [00:00<00:00, 14.97it/s, now=None]
Moviepy - Done !
Moviepy - video ready ../../exports/examples/connectivity_cicle.mp4